package org.jeecg.modules.exTenderAnnQuaInqueryAnn.service.impl;

import org.apache.shiro.SecurityUtils;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.exOperationRecord.entity.ExOperationRecord;
import org.jeecg.modules.exOperationRecord.service.IExOperationRecordService;
import org.jeecg.modules.exProject.constant.ExProjectStepEnum;
import org.jeecg.modules.exProject.entity.ExProject;
import org.jeecg.modules.exProject.service.IExProjectService;
import org.jeecg.modules.exProject.vo.ExProjectParam;
import org.jeecg.modules.exTenderAnnQuaInqueryAnn.entity.ExTenderAnnQuaInqueryAnn;
import org.jeecg.modules.exTenderAnnQuaInqueryAnn.entity.ExGcNoticesection;
import org.jeecg.modules.exTenderAnnQuaInqueryAnn.mapper.ExGcNoticesectionMapper;
import org.jeecg.modules.exTenderAnnQuaInqueryAnn.mapper.ExTenderAnnQuaInqueryAnnMapper;
import org.jeecg.modules.exTenderAnnQuaInqueryAnn.service.IExTenderAnnQuaInqueryAnnService;
import org.jeecg.modules.gcProject.vo.StatusEnum;
import org.jeecg.modules.website.entity.WebTradeNotice;
import org.jeecg.modules.website.mapper.WebTradeNoticeMapper;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.Date;
import java.util.List;
import java.util.Collection;

/**
 * @Description: ex_tender_ann_qua_inquery_ann
 * @Author: jeecg-boot
 * @Date:   2024-09-04
 * @Version: V1.0
 */
@Service
public class ExTenderAnnQuaInqueryAnnServiceImpl extends ServiceImpl<ExTenderAnnQuaInqueryAnnMapper, ExTenderAnnQuaInqueryAnn> implements IExTenderAnnQuaInqueryAnnService {

	@Autowired
	private ExTenderAnnQuaInqueryAnnMapper exTenderAnnQuaInqueryAnnMapper;
	@Autowired
	private ExGcNoticesectionMapper exGcNoticesectionMapper;
	@Autowired
	private WebTradeNoticeMapper webTradeNoticeMapper;
	@Autowired
	private IExOperationRecordService exOperationRecordService;
	@Autowired
	private IExProjectService iExProjectService;
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void saveMain(ExTenderAnnQuaInqueryAnn exTenderAnnQuaInqueryAnn, List<ExGcNoticesection> exGcNoticesectionList) {
		exTenderAnnQuaInqueryAnn.setStatus(StatusEnum.SAVE.getCode());
		exTenderAnnQuaInqueryAnnMapper.insert(exTenderAnnQuaInqueryAnn);
		if(exGcNoticesectionList!=null && exGcNoticesectionList.size()>0) {
			for(ExGcNoticesection entity:exGcNoticesectionList) {
				//外键设置
				entity.setNoticeid(exTenderAnnQuaInqueryAnn.getId());
				entity.setTenderProjectId(exTenderAnnQuaInqueryAnn.getProjectId());
				exGcNoticesectionMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void updateMain(ExTenderAnnQuaInqueryAnn exTenderAnnQuaInqueryAnn,List<ExGcNoticesection> exGcNoticesectionList) {
		exTenderAnnQuaInqueryAnnMapper.updateById(exTenderAnnQuaInqueryAnn);
		
		//1.先删除子表数据
		exGcNoticesectionMapper.deleteByMainId(exTenderAnnQuaInqueryAnn.getId());
		
		//2.子表数据重新插入
		if(exGcNoticesectionList!=null && exGcNoticesectionList.size()>0) {
			for(ExGcNoticesection entity:exGcNoticesectionList) {
				//外键设置
				entity.setNoticeid(exTenderAnnQuaInqueryAnn.getId());
				entity.setTenderProjectId(exTenderAnnQuaInqueryAnn.getProjectId());
				exGcNoticesectionMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delMain(String id) {
		exGcNoticesectionMapper.deleteByMainId(id);
		exTenderAnnQuaInqueryAnnMapper.deleteById(id);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			exGcNoticesectionMapper.deleteByMainId(id.toString());
			exTenderAnnQuaInqueryAnnMapper.deleteById(id);
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public boolean audit(ExProjectParam exProjectParam) {
		LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		ExTenderAnnQuaInqueryAnn entity = this.getById(exProjectParam.getId());
		if (entity == null) {
			throw new JeecgBootException("未找到对应数据");
		}
		entity.setStatus(exProjectParam.getStatus());
		entity.setUpdateTime(new Date());
		entity.setUpdateBy(sysUser.getUsername());
		this.updateById(entity);
		ExOperationRecord exOperationRecord = new ExOperationRecord();
		exOperationRecord.setEntryId(exProjectParam.getId());
		exOperationRecord.setModule("exTenderAnnQuaInqueryAnn");
		exOperationRecord.setOperateTime(new Date());
		exOperationRecord.setOperateResult(StatusEnum.SUCCESS.getCode().equals(exProjectParam.getStatus()) ? "1" : "2");//1 通过 2驳回
		exOperationRecord.setOperateOpinion(exProjectParam.getOperateOpinion());
		exOperationRecord.setCategory("1");
		exOperationRecord.setOperator(sysUser.getRealname());
		exOperationRecordService.save(exOperationRecord);

		// 审核通过则更新项目信息的步骤为招标公告
		ExProject exProject = iExProjectService.getById(entity.getProjectId());
		if (null != exProject && exProject.getStep() < ExProjectStepEnum.ZBGG.getValue() && StatusEnum.SUCCESS.getCode().equals(exProjectParam.getStatus())) {
			exProject.setStep(ExProjectStepEnum.ZBGG.getValue());

				WebTradeNotice webTradeNotice = new WebTradeNotice();
				webTradeNotice.setId(entity.getId());
				webTradeNotice.setCategory("sys_trade_type_jgcg");//建设工程大类
				/**缺行政区域标志  webTradeNotice.setArea(exTenderAnnQuaInqueryAnn.)**/
				webTradeNotice.setTitile(entity.getNoticeName());//公告标题
				webTradeNotice.setContent(entity.getNoticeContent());//公告内容
				webTradeNotice.setPublishTime(entity.getCreateTime());//发布时间
				webTradeNotice.setOpenType("component");//发布方式默认
				webTradeNotice.setOpenPage(entity.getUrl());//URL地址
				webTradeNotice.setPublisher("开封市公共资源交易信息网");//默认发布人
				webTradeNotice.setType(entity.getBulletinType());
				if (webTradeNotice.getType() == "2") {
					webTradeNotice.setTypeDesc("招标公告");
				}
				if (webTradeNotice.getType() == "3") {
					webTradeNotice.setTypeDesc("变更公告");
				}
				webTradeNotice.setPublishStatus("1");//默认已发布
				webTradeNotice.setPriority("H");//默认优先级高
				webTradeNotice.setArea(entity.getPlatformCode());//行政区域
				webTradeNoticeMapper.insert(webTradeNotice);
			}
			iExProjectService.updateById(exProject);

		return true;
	}

}
